<script type="text/html" template lay-done="layui.data.done(d);">
    <div class="layui-form coreshop-form layui-form-pane" lay-filter="LAY-app-hotbillboard-createForm" id="LAY-app-hotbillboard-createForm">

        <!--<div class="layui-form-item">
        <label for="id" class="layui-form-label  layui-form-required">编号</label>
        <div class="layui-input-block">
            <input  type="number" min="0" max="999999" name="id"   lay-verType="tips" lay-verify="required|number" class="layui-input" value="1" placeholder="请输入编号" lay-reqText="请输入编号并为数字"  />
        </div>
    </div>-->

        <div class="layui-form-item">
            <label for="title" class="layui-form-label  layui-form-required">标题</label>
            <div class="layui-input-block">
                <input name="title" lay-verType="tips" lay-verify="required" class="layui-input" placeholder="请输入标题" lay-reqText="请输入标题" />
            </div>
        </div>
        <div class="layui-form-item">
            <label for="thridCategoryId" class="layui-form-label layui-form-required">第三方分类</label>
            <div class="layui-input-inline layui-inline-6">
                <div id="thridCategoryId_SelectCreateTree" class="ew-xmselect-tree" lay-reqText="请选择第三方分类"></div>
            </div>
            <div class="layui-form-mid layui-word-aux">
                <button href="javascript:void(0);" id="add-class1" class="add-class layui-btn layui-btn-normal layui-btn-xs">添加分类</button>
            </div>

        </div>

        <div class="layui-form-item">
            <label for="categoryId" class="layui-form-label layui-form-required">商品分类</label>
            <div class="layui-input-inline layui-inline-6">
                <div id="categoryId_SelectCreateTree" class="ew-xmselect-tree" lay-reqText="请选择商品分类"></div>
            </div>
            <div class="layui-form-mid layui-word-aux">
                <button href="javascript:void(0);" id="add-class2"  class="add-class layui-btn layui-btn-normal layui-btn-xs">添加分类</button>
            </div>

        </div>

        <!--<div class="layui-form-item">
        <label for="creattime" class="layui-form-label  layui-form-required">创建日期</label>
        <div class="layui-input-block">
            <input name="creattime" id="createTime-hotbillboard-creattime" type="text" lay-verType="tips" lay-verify="required|datetime" class="layui-input" placeholder="请输入创建日期" lay-reqText="请输入创建日期" />
        </div>
    </div>-->

        <div class="layui-form-item">

            <!--<fieldset class="layui-elem-field layui-field-title site-title"><legend><a name="compress">封面图</a></legend></fieldset>-->

            <div class="layui-form-item">
                <label class="layui-form-label  layui-form-required" for="avatarImage">封面图：</label>
                <div class="layui-input-inline layui-inline-10">
                    <input name="image" id="fengmianImage" size="15" lay-verify="required" autocomplete="off" class="layui-input" lay-reqText="请输入【缩略图】" />
                </div>
                <div class="layui-input-inline">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="upBtn">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="viewImgBox" src="{{ layui.setter.noImagePicUrl }}">
                            <p id="viewTextBox"></p>
                        </div>
                    </div>
                </div>
            </div>

            <!--<fieldset class="layui-elem-field layui-field-title site-title"><legend><a name="compress">图集</a></legend></fieldset>-->
            <!--<div class="layui-form-item">
                <div class="layui-input-inline layui-inline-2">
                    <input type="hidden" name="images" id="imageIds" class="layui-input" placeholder="请上传缩略图" lay-reqText="请上传缩略图" />
                    <button type="button" class="layui-btn  layui-btn-sm" id="upBtnimageIds"><i class="layui-icon"></i>上传图片</button>
                </div>
                <div class="layui-form-mid">
                    图集：[建议数量控制在10张以内]：
                </div>
            </div>
            <div class="layui-form-item">
                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                    <div class="layui-upload-list" id="upload-more-list"></div>
                </blockquote>
            </div>-->

            <!--<fieldset class="layui-elem-field layui-field-title site-title"><legend><a name="compress">媒体</a></legend></fieldset>
        <div class="layui-form-item">
            <label class="layui-form-label" for="video">视频：</label>
            <div class="layui-input-inline layui-inline-10">
                <input name="goods[video]" size="15" autocomplete="off" class="layui-input" lay-reqText="请输入【视频地址】" />
            </div>
            <div class="layui-form-mid">支持常用视频播放格式。</div>
        </div>-->
        </div>




        <div class="layui-form-item text-right core-hidden">
            <input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-hotbillboard-createForm-submit" id="LAY-app-hotbillboard-createForm-submit" value="确认添加">
        </div>
    </div>
</script>

<script id="image_tpl" type="text/html">
    {{# layui.each(d, function(index, item){  }}
    <div class="imgdiv">
        <img src="{{ item }}" class="layui-upload-img" onclick='layui.coreHelper.viewImage("{{ item }}")' style="width: 100px;height:100px;">
        <div id="operate">
            <div><a class="del" onclick="delImg(this,'{{ item }}')">删除</a>|<a class="setmain" onclick="setDefault(this,'{{ item }}')">设为主图</a></div>
        </div>
    </div>
    {{#  }); }}
</script>

<script>
    var imgs = [];
    var debug = layui.setter.debug;

    layui.data.done = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d.params.data); }
        layui.use(['admin', 'laytpl', 'form', 'view', 'laydate', 'upload', 'coreHelper', 'cropperImg','xmSelect'],
        function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , laydate = layui.laydate
                , upload = layui.upload
                , laytpl = layui.laytpl
                , view = layui.view
                , cropperImg = layui.cropperImg
                , xmSelect = layui.xmSelect
                , coreHelper = layui.coreHelper;
	        
            laydate.render({
                elem: '#createTime-hotbillboard-creattime',
                type: 'datetime'
            });
	        form.verify({
                
            });

          
            //获取三方分类
            coreHelper.Get("Api/HotbillBoard/GetThirdCategory", function (e) {
            
                //table.reloadData('LAY-app-CoreCmsGoods-tableBox');
             
                // 渲染商品分类
                var insXmSelThridCategoryId = xmSelect.render({
                    el: '#thridCategoryId_SelectCreateTree',
                    height: '150px',
                    data: e.data,
                    //initValue: mData ? [mData.parentId] : [],
                    //model: { label: { type: 'text' } },
                    name: 'thridCategoryId',
                    prop: {
                        name: 'categoryName',
                        value: 'id'
                    },
                    //layVerify: 'verifyGoodsCategory',
                    //layVerType: 'msg',
                    radio: true,
                    clickClose: true,
                    tree: {
                        show: true,
                        indent: 15,
                        strict: false,
                        expandedKeys: true
                    }
                });

                //添加分类
                $(document).on('click', '#add-class1', function () {
                    admin.popup({
                        shadeClose: false,
                        title: '添加第三方分类',
                        area: ['700px', '540px'],
                        id: 'LAY-app-hotbillboardthridcategory-createForm',
                        success: function (layero, index) {
                            view(this.id).render('hotbillboard/category/create', {}).done(function () {
                                //监听提交

                                form.on('submit(LAY-app-hotbillboardthridcategory-createForm-submit)',
                                    function (data) {
                                        var field = data.field; //获取提交的字段
                                        field.isShow = field.isShow == 'on';
                                        if (debug) { console.log(field); } //开启调试返回数据
                                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                                        coreHelper.Post("Api/HotbillBoard/DoCreateThirCate", field, function (e) {
                                            if (e.code === 0) {
                                                //重新渲染分类下拉
                                                insXmSelThridCategoryId.update({ data: e.data, autoRow: true, })

                                                form.render();
                                                layer.close(index); //再执行关闭
                                                layer.msg(e.msg);
                                            } else {
                                                layer.msg(e.msg);
                                            }
                                        });
                                    });
                            });
                        }
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            layero.contents().find("#LAY-app-hotbillboardthridcategory-createForm-submit").click();
                        }
                    });
                });


                ///
                form.render();
              
            });
         
            //获取商品分类
            coreHelper.Post("Api/CoreCmsGoods/GetIndex", {},function (e) {
                console.log("111111111",e)
        
                // 渲染商品分类
                var insXmSelGoodsCategoryId = xmSelect.render({
                    el: '#categoryId_SelectCreateTree',
                    height: '150px',
                    data: e.data.categories.data,
                    //initValue: mData ? [mData.parentId] : [],
                    //model: { label: { type: 'text' } },
                    name: 'categoryId',
                    prop: {
                        name: 'title',
                        value: 'id'
                    },
                    //layVerify: 'verifyGoodsCategory',
                    //layVerType: 'msg',
                    radio: true,
                    clickClose: true,
                    tree: {
                        show: true,
                        indent: 15,
                        strict: false,
                        expandedKeys: true
                    }
                });

                //添加分类
                $(document).on('click', '#add-class2', function () {
                    coreHelper.Post("Api/CoreCmsGoodsCategory/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '添加分类',
                                area: ['700px', '540px'],
                                id: 'LAY-popup-CoreCmsGoodsCategory-create',
                                success: function (layero, index) {
                                    view(this.id).render('good/goodscategory/create', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsGoodsCategory-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段
                                                field.isShow = field.isShow == 'on';
                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsGoodsCategory/DoCreate", field, function (e) {
                                                    if (e.code === 0) {
                                                        //重新渲染分类下拉
                                                        insXmSelGoodsCategoryId.update({ data: e.data.categories.data, autoRow: true, })
                                                    
                                                        form.render();
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsGoodsCategory-createForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                });



                ///
                form.render();

            });


         
          

            //封面图图片上传
            $('#upBtn').click(function () {
                cropperImg.cropImg({
                    aspectRatio: 1 / 1,
                    imgSrc: $('#viewImgBox').attr('src'),
                    onCrop: function (data) {
                        var loadIndex = layer.load(2);
                        coreHelper.Post("api/Tools/UploadFilesFByBase64", { base64: data }, function (res) {
                            if (0 === res.code) {
                                $('#viewImgBox').attr('src', res.data.fileUrl);
                                $("#fengmianImage").val(res.data.fileUrl);
                                layer.msg(res.msg);
                                layer.close(loadIndex);
                            } else {
                                layer.close(loadIndex);
                                layer.msg(res.msg, { icon: 2, anim: 6 });
                            }
                        });
                    }
                });
                form.render();
            });

            //图集上传
            $('#upBtnimageIds').click(function () {
                cropperImg.cropImg({
                    aspectRatio: 1 / 1,
                    onCrop: function (data) {
                        var loadIndex = layer.load(2);
                        coreHelper.Post("api/Tools/UploadFilesFByBase64", { base64: data }, function (res) {
                            if (0 === res.code) {
                                imgs.push(res.data.fileUrl);
                                $('#imageIds').val(imgs);
                                var getTpl = image_tpl.innerHTML, view = document.getElementById('upload-more-list');
                                laytpl(getTpl).render(imgs, function (html) {
                                    view.innerHTML = html;
                                });
                                layer.msg(res.msg);
                                layer.close(loadIndex);
                            } else {
                                layer.close(loadIndex);
                                layer.msg(res.msg, { icon: 2, anim: 6 });
                            }
                        });
                    }
                });
                form.render();
            });

            //SKU缩略图上传
            $('.upSpecImage').click(function () {
                var _that = this;
                cropperImg.cropImg({
                    aspectRatio: 1 / 1,
                    imgSrc: 'no',
                    onCrop: function (data) {
                        var loadIndex = layer.load(2);
                        coreHelper.Post("api/Tools/UploadFilesFByBase64", { base64: data }, function (res) {
                            if (0 === res.code) {
                                var item = $(_that);
                                item.prev().find('#productImagesInput').val(res.data.fileUrl);
                                item.prev().find('#productImagesSrc').attr("src", res.data.fileUrl);
                                layer.msg(res.msg);
                                layer.close(loadIndex);
                            } else {
                                layer.close(loadIndex);
                                layer.msg(res.msg, { icon: 2, anim: 6 });
                            }
                        });
                    }
                });
                form.render();
            });

            //重载form
            form.render(null, 'LAY-app-hotbillboard-createForm');

            })
    };


    //删除图片
    function delImg(obj, imageUrl) {
        var imgDiv = $(obj).parent().parent().parent();
        imgDiv.remove();
        var index = imgs.indexOf(imageUrl);
        if (index > -1) {
            imgs.splice(index, 1);
        }
        $('#imageIds').val(imgs);
    }
    //设为默认图
    function setDefault(obj, imageUrl) {
        var imgDiv = $(obj).parent().parent().parent();
        $("#upload-more-list").prepend(imgDiv);
        var index = imgs.indexOf(imageUrl);
        if (index > -1) {
            imgs.unshift(imgs.splice(index, 1)[0]);
        }
        $('#imageIds').val(imgs);
    }
</script>
